package com.example.demo.socket;


import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


/**
 * @author yj
 * @date 2021/4/6 13:40
 */
@Sharable
public class SocketServerUserEventTriggeredHandler extends ChannelInboundHandlerAdapter {

    private Logger logger = LoggerFactory.getLogger(SocketServerUserEventTriggeredHandler.class);

    //异常类型  READER_IDLE-读超时  WRITER_IDLE-写超时  ALL_IDLE-读写超时
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
        Channel channel = ctx.channel();
        String shortId = channel.id().asShortText();
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            switch (event.state()) {
                case ALL_IDLE:
                    logger.error("链接：{}，读写超时", shortId);
                    break;
                case READER_IDLE:
                    logger.error("链接：{}，读超时", shortId);
                    break;
                default:
                    logger.error("链接：{}，写超时", shortId);
            }
            channel.close();
        }
    }
}